package com.exploadingelephants.views;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import ch.exploadingelephants.graph.DotColor;
import ch.exploadingelephants.graph.DotString;

import com.exploadingelephants.R;

/**
 * @author Sandro De Zanet
 * 
 */
public class DotStringView extends View {

    private DotString dotString;
    private final Map<DotColor, Bitmap> dot2Bitmap = new HashMap<DotColor, Bitmap>();
    private final Bitmap band_start, band_end, band_element;

    private final int fieldWidth;
    private final int fieldHeight;
    private final int xOffset;

    /**
     * @param context
     * @param dotString
     */
    public DotStringView(Context context, DotString dotString) {
	super(context);
	setWillNotDraw(false);
	this.dotString = dotString;
	dot2Bitmap.put(DotColor.RED, BitmapFactory.decodeResource(
		getResources(), R.drawable.red_dot));
	dot2Bitmap.put(DotColor.BLUE, BitmapFactory.decodeResource(
		getResources(), R.drawable.blue_dot));
	band_start = BitmapFactory.decodeResource(getResources(),
		R.drawable.band_start);
	band_end = BitmapFactory.decodeResource(getResources(),
		R.drawable.band_end);
	band_element = BitmapFactory.decodeResource(getResources(),
		R.drawable.band_element);
	this.fieldWidth = dot2Bitmap.get(DotColor.RED).getWidth();
	this.fieldHeight = dot2Bitmap.get(DotColor.RED).getHeight();
	xOffset = band_start.getWidth();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
	int counter = 0;
	canvas.drawBitmap(band_start, 0, 0, null);

	int yOffset = (band_start.getHeight() - fieldHeight) / 2;
	for (DotColor color : dotString) {
	    for (int i = 0; i < (fieldWidth / band_element.getWidth()) + 1; i++) {
		canvas.drawBitmap(band_element, xOffset + counter
			* (fieldWidth + band_element.getWidth()) + i
			* band_element.getWidth(), 0, null);
	    }
	    canvas.drawBitmap(dot2Bitmap.get(color), xOffset + counter
		    * (fieldWidth + band_element.getWidth()), yOffset, null);
	    counter++;
	}
	canvas.drawBitmap(band_end, xOffset + dotString.size()
		* (fieldWidth + band_element.getWidth()), 0, null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View#onTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
	System.out.println(event);
	boolean superEvent = super.onTouchEvent(event);
	return superEvent;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View#onMeasure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	setMeasuredDimension(band_start.getWidth() + band_end.getWidth()
		+ (fieldWidth + band_element.getWidth()) * dotString.size(),
		band_start.getHeight());
    }

    /**
     * @param string
     *            new string to display
     * 
     */
    public void setDotString(DotString string) {
	this.dotString = string;
	invalidate();
    }
}
